package com.xialinrui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xialinrui.entity.*;
import com.xialinrui.service.ExpenditureCategoryService;
import com.xialinrui.service.ExpenditureService;
import com.xialinrui.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xialinrui
 */
@RestController
@Slf4j
@RequestMapping("/expenditure")
public class ExpenditureController {
    @Resource
    private ExpenditureService expenditureService;

    @Resource
    private ExpenditureCategoryService expenditureCategoryService;

    @Resource
    private UserService userService;

    @PostMapping("/add")
    public ResponseResult<Map<String, Long>> add(@RequestBody Expenditure expenditure) {
        HashMap<String, Long> resMap = new HashMap<>(1);
        expenditureService.addExpenditure(expenditure);
        resMap.put("id", expenditure.getId());
        return new ResponseResult(200, "添加成功", resMap);
    }

    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestBody List<Long> ids) {
        expenditureService.deleteByIds(ids);
        return new ResponseResult(200, "删除成功");
    }

    @GetMapping("/list")
    public ResponseResult<Page<Expenditure>> list(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam Long id) {
        Page<Expenditure> resPage = new Page<>(page, pageSize);
        QueryWrapper<Expenditure> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("expenditure_category_id", id).orderByDesc("date");
        expenditureService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestBody Expenditure expenditure) {
        //查询出用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id", 1);
        User user = userService.getOne(userQueryWrapper);
        //查询出旧的expenditure
        Expenditure old = expenditureService.getById(expenditure.getId());
        //修改钱数
        user.setWholeMoney(user.getWholeMoney() - old.getMoney() + expenditure.getMoney());
        expenditureService.updateById(expenditure);
        return new ResponseResult(200, "更新成功");
    }

    @GetMapping("/category/list")
    public ResponseResult<List<ExpenditureCategory>> category() {
        QueryWrapper<ExpenditureCategory> expenditureCategoryQueryWrapper = new QueryWrapper<>();
        expenditureCategoryQueryWrapper.ne("id",2);
        return new ResponseResult<>(200, expenditureCategoryService.list(expenditureCategoryQueryWrapper));
    }

    @PostMapping("/category/add")
    public ResponseResult<Map<String, Long>> addCategory(@RequestBody ExpenditureCategory expenditureCategory) {
        HashMap<String, Long> resMap = new HashMap<>(1);
        expenditureCategoryService.save(expenditureCategory);
        resMap.put("id", expenditureCategory.getId());
        return new ResponseResult(200, "添加成功", resMap);
    }

    @DeleteMapping("/category/delete")
    public ResponseResult deleteCategory(@RequestBody List<Long> ids) {
        expenditureCategoryService.deleteByIds(ids);
        return new ResponseResult(200, "删除成功");
    }

    @PutMapping("/category/update")
    public ResponseResult updateCategory(@RequestBody ExpenditureCategory expenditureCategory) {
        expenditureCategoryService.updateById(expenditureCategory);
        return new ResponseResult(200, "更新成功");
    }

}
